package com.example.dou2204;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.dou2204.ban.BannerViewModel;
import com.example.dou2204.baseMVVM.BaseActivity;
import com.example.dou2204.databinding.ActivityMainBinding;
import com.example.dou2204.entity.BannerEntity;
import com.example.dou2204.entity.UserEntity;
import com.example.dou2204.login.LoginActivity;
import com.example.dou2204.register.RegisterViewModel;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 第一步：先实现基础的注册功能
 * 第二步：添加验证绑定   ---实现非空校验，手机号校验
 * 第三步：验证码  -初始化界面时，注册按钮不可用，输入验证码后 按钮可用
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, RegisterViewModel> {
        Timer timer;
        Handler handler = new Handler(){};
        int time = 60;

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity entity) {
                if(entity.getCode()==200){
                    Toast.makeText(MainActivity.this, ""+entity.getMessage(), Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this, LoginActivity.class));
                }else {
                    Toast.makeText(MainActivity.this, ""+entity.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(MainActivity.this, ""+s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initView() {
        binding.btnRegister.setEnabled(false);//按钮不用了
        //监控验证码文本，控制按钮是否可用
        binding.etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = binding.etCode.getText().toString().length();
                if(length==6){
                    binding.btnRegister.setEnabled(true);
                }else {
                    binding.btnRegister.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //获取验证码--正常应发送短信的形式--暂时用随机代替 ----点击获取验证码后，按钮不可以，显示60秒后重新获取//如何判断自己输入的验证码是最后一次发出的验证码
        binding.btnGetcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = new Random().nextInt(999999);
                ToastUtils.showLong(i);
                //将验证进行存储
                SPUtils.getInstance().put("code",i+"");
                //开启定时任务
                binding.btnGetcode.setEnabled(false);
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                binding.btnGetcode.setText(time--+"秒后再获取");
                                if(time==0){
                                    binding.btnGetcode.setEnabled(true);
                                    binding.btnGetcode.setText("获取验证码");
                                    timer.cancel();
                                    time= 60;//重新给倒计时赋值
                                }
                            }
                        });
                    }
                },0,1000);
            }
        });
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etName.getText().toString();
                String password = binding.etPwd.getText().toString();
                String repassword = binding.etRepwd.getText().toString();
                String code = binding.etCode.getText().toString();
                if(username.isEmpty()||password.isEmpty()||repassword.isEmpty()){
                    Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!password.equals(repassword)){
                    Toast.makeText(MainActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!RegexUtils.isMobileExact(username)){
                    ToastUtils.showLong("用户名格式必须为手机号");
                    return;
                }
                //判断验证是否正确
                if(!code.equals(SPUtils.getInstance().getString("code"))){
                    ToastUtils.showLong("验证码不正确，请重新输入");
                    return;
                }
                viewModel.register(binding.etName.getText().toString(),
                        binding.etPwd.getText().toString(),
                        binding.etRepwd.getText().toString()
                );
            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }
}




